from rest_framework import serializers

from course import models


class CourseCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = models.CourseCategory
        fields = ['id', 'name']


class TeacherSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Teacher
        # fields = ['name', 'title', 'role_name']
        fields = ('name', 'role_name', 'title', 'signature', 'image', 'brief')


class CourseModelSerializer(serializers.ModelSerializer):
    # 获取teacher字段有三种方式，第一种用 serializers.SerializerMethodField,然后写一个get_teacher的方法
    # teacher = instance.teacher.objects.filter(is_delete=False, is_show=True).order_by('orders')
    # teacher = serializers.SerializerMethodField()
    # 第二种，在models文件中写一个获取老师的方法
    # 第三种，子序列化,先写一个teacher的序列化类
    teacher = TeacherSerializer()

    # def get_teacher(self, instance):
    #     """
    #     :param instance: 就是类视图里queryset获取到的对象，在这里是course表
    #     :return:
    #     """
    #
    #     return

    class Meta:
        model = models.Course
        # fields = ['id',
        #           'name',
        #           'course_img',
        #           'brief',
        #           'price',
        #           'teacher',
        #           'course_type_name',
        #           'level_choices_name',
        #           'status_name',
        #           'course_sections',
        #           ]
        fields = (
            'id',
            'name',
            'course_img',
            'brief',
            'attachment_path',
            'pub_sections',
            'price',
            'students',
            'period',
            'sections',
            'course_type_name',
            'level_name',
            'status_name',
            'teacher',
            'section_list',
        )


# 课时序列化类
class CourseSectionSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.CourseSection
        fields = ['name', 'orders', 'duration', 'free_trail', 'section_link', 'section_type_name']


class CourseChapterSerializer(serializers.ModelSerializer):
    # 添加课时字段，跟课程表里的teacher字段添加方法一样
    coursesections = CourseSectionSerializer(many=True, read_only=True)

    class Meta:
        model = models.CourseChapter
        fields = ['name', 'summary', 'chapter', 'coursesections']